package org.liuh.designs.spider.utils;

import java.util.Random;

/**
 * @ClassName: RandomUtils
 * @Description:
 * @Author: liuh
 * @Date: 2020/10/10 16:04
 * @Version: 1.0
 */
public class RandomUtils {
    private static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LOW_NUMBER = "0123456789abcdefghijklmnopqrstuvwxyz";
    private static final String LOW_NUMBERAND_ = "0123456789abcdefghijklmnopqrstuvwxyz_";
    private static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String NUMBER_CHAR = "0123456789";

    /**
     * @Description: 获取定长的随机数，包含大小写、数字
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length())));
        }
        return sb.toString();
    }

    /**
     * @Description: 小写字母 + 数字
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static String generateLowNumberString(int len) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            sb.append(LOW_NUMBER.charAt(random.nextInt(LOW_NUMBER.length())));
        }
        return sb.toString();
    }
    /**
     * @Description: 小写字母 + 数字 + _
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static String generateLowNumberAnd_String(int len) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            sb.append(LOW_NUMBER.charAt(random.nextInt(LOW_NUMBER.length())));
        }
        return sb.toString();
    }


    /**
     * @Description: 获取定长的随机数, 包含大小写字母
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static String generateMixString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(LETTER_CHAR.charAt(random.nextInt(LETTER_CHAR.length())));
        }
        return sb.toString();
    }

    /**
     * @Description: 获取定长的随机数，只包含小写字母
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static String generateLowerString(int length) {
        return generateMixString(length).toLowerCase();
    }

    /**
     * @Description: 获取定长的随机数, 只包含大写字母
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static String generateUpperString(int length) {
        return generateMixString(length).toUpperCase();
    }

    /**
     * @Description: 获取定长的随机数, 只包含数字
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static String generateNumberString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
        }
        return sb.toString();
    }

    /**
     * @Description:
     * @Author: liuh
     * @Date: 2020/10/10
     */
    public static int getRandomNumber(int max) {
        Random random = new Random();
        return random.nextInt(max);
    }
}
